﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>object functions</title>
</head>
<body>
    <script type="text/javascript">
        var array = {
            concat: function () {
                var a = ['a', 'b', 'c'];
                var b = ['x', 'y', 'z'];
                return a.concat(b, true); //is ['a', 'b', 'c', 'x', 'y', 'z', true]
            }(),
            join: function () {
                var a = ['a', 'b', 'c'];
                a.push('d');
                return a.join('');    // c is 'abcd';
            }(),
            pop: function () {
                var a = ['a', 'b', 'c'];
                return a.pop();    // a is ['a', 'b'] & return 'c'
            }(),
            push: function () {
                var a = ['a', 'b', 'c'];
                var b = ['x', 'y', 'z'];
                return a.push(b, true);
                // a is ['a', 'b', 'c', ['x', 'y', 'z'], true]
            }(),
            reverse: function () {
                var a = ['a', 'b', 'c'];
                return a.reverse();
            }(),
            shift: function () {
                var a = ['a', 'b', 'c'];
                return a.shift();    // a is ['b', 'c'] & return is 'a'
            }(),
            slice: function () {
                var a = ['a', 'b', 'c'];
                var b = a.slice(0, 1);    // b is ['a']
                var c = a.slice(1);       // c is ['b', 'c']
                var d = a.slice(1, 2);    // d is ['b']
                return b.length == 1 && c.length == 2 && d.pop() == 'b';
            }(),
            sort: function () {
                var n = [4, 8, 15, 16, 23, 42];
                n.sort();
                // n is [15, 16, 23, 4, 42, 8]

                n.sort(function (a, b) {
                    return a - b;
                });
                return n.shift() == 4;
            }(),
            splice: function () {
                //array.splice(start, deleteCount, item...)
                var a = ['a', 'b', 'c'];
                var r = a.splice(1, 1, 'ache', 'bug');
                return a;
                // a is ['a', 'ache', 'bug', 'c']
                // r is ['b']
            }(),
            unshift: function () {
                var a = ['a', 'b', 'c'];
                a.unshift('?', '@');
                return a.shift() == '?';
                // a is ['?', '@', 'a', 'b', 'c']
                // r is 5
            }(),
        };
        var func = {
            apply: function () {
                var upper = String.prototype.toUpperCase;
                return upper.apply('a') == 'A';
            }(),
            applyWithArgs: function () {
                var substring = String.prototype.substring;
                return substring.apply('pato', [0, 1]) == 'p';
            }(),
        };       
    </script>
</body>
</html>
